package com.macrolab.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.macrolab.util.GDAO;

import java.sql.SQLException;

public class DataSourceConfig {

//    /**
//     * 代码生成的package位置
//     */
//    public static String packageBase = "com.stylefeng.guns.modular.hatchet";
//
//    public static String dbSchema = "fm0522";
//    public static String dbUsername = "root";
//    public static String dbPassword = "123456";
//
//    // 数据字典比较的生成结果文件名，SyncDict.groovy脚本使用此文件向本地库导入字典
//    public static String dictCmpResultFile_onlyInRemote = "数据字典比较-只存在于远端.txt";
//    public static String dictCmpResultFile_onlyInLocal = "数据字典比较-只存在于本地.txt";

    /**
     * 定位输出目录到 本项目 guns-generator的同级目录output上
     *
     * @return D:\mySCM\hatchet\gitee\mintingFM\
     */
//    public static String getProjectRootPath() {
//        String outpath = System.getProperty("user.dir"); // 当前路径
//        String projName = "guns-generator";
//        outpath = outpath.split(projName)[0];
//        return outpath;
//    }
    /**
     * 生成代码参照的数据库
     */

    public static GDAO getDAO(String dbHost, String dbPort, String dbSchema, String dbUsername, String dbPassword) throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();
        DruidProperties druidProperties = new DruidProperties();
        druidProperties.setUrl("jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbSchema + "?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull");
        druidProperties.setUsername(dbUsername);
        druidProperties.setPassword(dbPassword);
        druidProperties.config(dataSource);
        GDAO dao = new GDAO(dataSource, true);

        // 连接测试
        dao.queryFirstrow("select now() ts from dual");
        return dao;
    }


    /**
     * 连接本地数据库
     */
    public static GDAO getLocalDAO() throws SQLException {
        String dbSchema = "tsm";
        String dbUsername = "root";
        String dbPassword = "123456";
        String dbHost = "127.0.0.1";
        String dbPort = "3306";
        return getDAO(dbHost, dbPort, dbSchema, dbUsername, dbPassword);
    }

    /**
     * 连接远程数据库
     */
    public static GDAO getRemoteDAO() throws SQLException {
        String dbSchema = "tsm";
        String dbUsername = "root";
        String dbPassword = "123456";
        String dbHost = "192.168.2.161";
        String dbPort = "3306";
        return getDAO(dbHost, dbPort, dbSchema, dbUsername, dbPassword);
    }
}